# Copyright 2020 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load(
    "//pw_build:pigweed.bzl",
    "pw_facade",
)

package(default_visibility = ["//visibility:public"])

licenses(["notice"])

constraint_setting(
    name = "backend_constraint_setting",
)

# TODO: pwbug.dev/328679085 - Remove this alias once no-one uses it.
alias(
    name = "facade",
    actual = "pw_malloc.facade",
)

COMMON_LINKOPTS = [
    # Link options that replace dynamic memory operations in standard
    # library with the pigweed malloc.
    "-Wl,--wrap=malloc",
    "-Wl,--wrap=free",
    "-Wl,--wrap=realloc",
    "-Wl,--wrap=calloc",
    "-Wl,--wrap=_malloc_r",
    "-Wl,--wrap=_realloc_r",
    "-Wl,--wrap=_free_r",
    "-Wl,--wrap=_calloc_r",
]

pw_facade(
    name = "pw_malloc",
    srcs = ["malloc.cc"],
    hdrs = [
        "public/pw_malloc/config.h",
        "public/pw_malloc/malloc.h",
    ],
    backend = ":backend",
    includes = ["public"],
    linkopts = COMMON_LINKOPTS,
    deps = [
        ":config_override",
        "//pw_allocator:allocator",
        "//pw_allocator:synchronized_allocator",
        "//pw_allocator:tracking_allocator",
        "//pw_assert",
        "//pw_bytes",
        "//pw_preprocessor",
    ],
)

label_flag(
    name = "config_override",
    build_setting_default = "//pw_build:default_module_config",
)

label_flag(
    name = "backend",
    build_setting_default = ":backend_multiplexer",
)

constraint_value(
    name = "best_fit_block_allocator_backend",
    constraint_setting = "//pw_malloc:backend_constraint_setting",
)

constraint_value(
    name = "bucket_block_allocator_backend",
    constraint_setting = "//pw_malloc:backend_constraint_setting",
)

constraint_value(
    name = "dual_first_fit_block_allocator_backend",
    constraint_setting = "//pw_malloc:backend_constraint_setting",
)

constraint_value(
    name = "first_fit_block_allocator_backend",
    constraint_setting = "//pw_malloc:backend_constraint_setting",
)

constraint_value(
    name = "freelist_backend",
    constraint_setting = "//pw_malloc:backend_constraint_setting",
)

constraint_value(
    name = "last_fit_block_allocator_backend",
    constraint_setting = "//pw_malloc:backend_constraint_setting",
)

constraint_value(
    name = "worst_fit_block_allocator_backend",
    constraint_setting = "//pw_malloc:backend_constraint_setting",
)

alias(
    name = "backend_multiplexer",
    actual = select({
        "//pw_malloc:best_fit_block_allocator_backend": "//pw_malloc:best_fit_block_allocator",
        "//pw_malloc:bucket_block_allocator_backend": "//pw_malloc:bucket_block_allocator",
        "//pw_malloc:dual_first_fit_block_allocator_backend": "//pw_malloc:dual_first_fit_block_allocator",
        "//pw_malloc:first_fit_block_allocator_backend": "//pw_malloc:first_fit_block_allocator",
        "//pw_malloc:last_fit_block_allocator_backend": "//pw_malloc:last_fit_block_allocator",
        "//pw_malloc:worst_fit_block_allocator_backend": "//pw_malloc:worst_fit_block_allocator",
        "//pw_malloc_freelist:backend": "//pw_malloc:bucket_block_allocator",
        "//pw_malloc_freertos:backend": "//pw_malloc_freertos",
        "//conditions:default": "//pw_build:unspecified_backend",
    }),
    visibility = ["//targets:__pkg__"],
)

cc_library(
    name = "best_fit_block_allocator",
    srcs = ["best_fit_block_allocator.cc"],
    deps = [
        ":pw_malloc.facade",
        "//pw_allocator:best_fit_block_allocator",
    ],
)

cc_library(
    name = "bucket_block_allocator",
    srcs = ["bucket_block_allocator.cc"],
    deps = [
        "//pw_allocator:bucket_block_allocator",
        "//pw_malloc:facade",
    ],
)

cc_library(
    name = "dual_first_fit_block_allocator",
    srcs = ["dual_first_fit_block_allocator.cc"],
    deps = [
        "//pw_allocator:dual_first_fit_block_allocator",
        "//pw_malloc:facade",
    ],
)

cc_library(
    name = "first_fit_block_allocator",
    srcs = ["first_fit_block_allocator.cc"],
    deps = [
        "//pw_allocator:first_fit_block_allocator",
        "//pw_malloc:facade",
    ],
)

cc_library(
    name = "last_fit_block_allocator",
    srcs = ["last_fit_block_allocator.cc"],
    deps = [
        "//pw_allocator:last_fit_block_allocator",
        "//pw_malloc:facade",
    ],
)

cc_library(
    name = "worst_fit_block_allocator",
    srcs = ["worst_fit_block_allocator.cc"],
    deps = [
        "//pw_allocator:worst_fit_block_allocator",
        "//pw_malloc:facade",
    ],
)

# TODO(b/343531095): Add unit tests to Bazel build whenever the following
# conditions are met:
# * The backend is not set.
# * The host is not "mac", due to missing linker support.
# * No sanitizer is configured, due to conflicts with interceptors.
# * pico_malloc is not in use.
# * gtest is not in use, since it dynamically allocates before calling SetUp().
filegroup(
    name = "malloc_test",
    srcs = [
        "malloc_test.cc",
        "pw_malloc_private/test_config_overrides.h",
    ],
)
